//
//  QZLeftPopVC.m
//  map
//
//  Created by 000 on 16/7/28.
//  Copyright © 2016年 faner. All rights reserved.
//

#import "QZLeftPopVC.h"
#import "QZAuthorTool.h"
#import "QZImageModal.h"
//#import "MapVC.h"

@interface QZLeftPopVC ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (nonatomic, assign) int i;
- (IBAction)cameraBtnClick;
- (IBAction)libraryBtnClick;
- (IBAction)backBtnClick;
- (IBAction)queDing;
@end

@implementation QZLeftPopVC

- (QZImageModal *)imageModal
{
    if (_imageModal == nil) {
        _imageModal = [[QZImageModal alloc] init];
        _imageModal.image = [UIImage imageNamed:@"add"];
    }
    return _imageModal;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.alpha = 0.3;
    self.i=0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)cameraBtnClick {
    [self visitSystemCamera];
}

- (IBAction)libraryBtnClick {
    [self visitSystimLibrary];
}

- (IBAction)backBtnClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)queDing {
//    NSLog(@"%d",self.i);
    if ([self.delegate respondsToSelector:@selector(leftPopVC:didQuedingBtnClickWithImage: andI:)]) {
        [self.delegate leftPopVC:self didQuedingBtnClickWithImage:self.imageModal.image andI:self.i];
        [self dismissViewControllerAnimated:YES completion:nil];
    }

    
}
- (void)visitSystimLibrary
{
    //判断相册权限
    [QZAuthorTool authorToolAccessLibrarySuccess:^{
        //打开相册
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate =self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.allowsEditing = YES;
        [self presentViewController:imagePicker animated:YES completion:nil];
    } fail:^{
        [self showAuthorAlertWithTypeName:@"相册"];
    }];
}
//访问系统相机
- (void)visitSystemCamera
{
    //判断相机的权限
    [QZAuthorTool authorToolAccessCameraSuccess:^{
        //打开相机
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:imagePicker animated:YES completion:nil];
    } fail:^{
        [self showAuthorAlertWithTypeName:@"相机"];
    }];
    
}

//根据不同的typeName（相册 相机） 显示不同的权限提示
- (void)showAuthorAlertWithTypeName:(NSString *)typeName
{
    //程序的名称
    NSString *appName = [[NSBundle mainBundle] infoDictionary][@"CFBundleName"];
    //提示的内容
    NSString *titleString = [NSString stringWithFormat:@"请在%@的“设置－隐私－%@”选项中，允许%@访问您的手机。",[UIDevice currentDevice].model,typeName,appName];
    
    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:titleString
                              message:nil
                              delegate:nil
                              cancelButtonTitle:@"知道了"
                              otherButtonTitles:nil];
    [alertView show];
}
- (UIImage *)imageWithImageSimple:( UIImage *)image scaledToSize:( CGSize )newSize{
    UIGraphicsBeginImageContext (newSize);
    [image drawInRect : CGRectMake ( 0 , 0 ,newSize. width ,newSize. height )];
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext ();
    UIGraphicsEndImageContext ();
    return newImage;
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image = info[@"UIImagePickerControllerOriginalImage"];
    UIImage *newImage = [self imageWithImageSimple:image scaledToSize:CGSizeMake(140, 220)];
    //将图片封装成QZImageModal对象
    QZImageModal *imageModal = [QZImageModal imageModalWithDictionary:@{@"image":newImage}];
    //将imageModal赋值给editView的imageModal属性
    self.imageModal = imageModal;
    self.i++;

//    self.i++;
//    self.i =self.i>0?1:0;
//    NSLog(@"12234555%d",self.i);
//    NSLog(@"%d",self.i);
    //相当于调用imageModal属性的setter方法[self.editView setImageModal:imageModal];
    
    //隐藏相册
    [self dismissViewControllerAnimated:YES completion:^{
        nil;
    }];
    
}

@end
